package com.miaoxun.web.aspect;

import com.miaoxun.domain.enums.ErrEnum;
import com.miaoxun.domain.exception.ThException;
import com.miaoxun.common.log.LogHelper;
import com.miaoxun.domain.model.ResultModel;
import com.miaoxun.web.shiro.ShiroUtils;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.exceptions.TemplateInputException;


import javax.servlet.http.HttpServletRequest;

@Component
@ControllerAdvice
public class ErrHandler {


    /**
     * 自定义异常
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultModel handleBDException(Exception e) {
        if (e instanceof ThException) {
            LogHelper.INFO("发生系统异常{}", ((ThException) e).getErrEnum().getCode());
            return new ResultModel(((ThException) e).getErrEnum().getCode(), ((ThException) e).getErrEnum().getMsg());

        } else if (e instanceof ShiroException) {
            LogHelper.INFO("权限异常{}", e.getMessage(), ErrHandler.class.getName());
            return new ResultModel(-1, e.getMessage());
        } else if (e instanceof org.springframework.validation.BindException) {
            LogHelper.INFO("验证异常{}", ((BindException) e).getAllErrors().get(0).getDefaultMessage(), "");
            return new ResultModel(-1, ((BindException) e).getAllErrors().get(0).getDefaultMessage());
        } else {
            LogHelper.INFO("发生不可控系统异常{}", e.getMessage(), ErrHandler.class.getName());
            return new ResultModel(-1, e.getMessage());
        }
    }

    @ExceptionHandler(ShiroException.class)
    @ResponseBody
    public Object handlePerJsonException(Exception e, HttpServletRequest request) {
        LogHelper.INFO("权限异常{}", "权限", ErrHandler.class.getName());
        if (request != null && request.getHeader("Accept").indexOf("text/html") != -1) {
            if (ShiroUtils.getUser() == null) {
                return "/login";
            } else {
                return "/error/403";
            }
        } else {
            if (e instanceof IncorrectCredentialsException || e instanceof UnknownAccountException
                    || e instanceof LockedAccountException) {
                return ResultModel.SUCCESS(ErrEnum.NOPER.getCode(), e.getLocalizedMessage());
            }

            if (ShiroUtils.getUser() == null) {
                return ResultModel.SUCCESS(ErrEnum.LOGIN.getCode(), ErrEnum.LOGIN.getMsg());
            } else {

                return ResultModel.SUCCESS(ErrEnum.NOPER.getCode(), ErrEnum.NOPER.getMsg());
            }
        }
    }

    @ExceptionHandler(DuplicateKeyException.class)
    @ResponseBody
    public ResultModel handleTemException(DuplicateKeyException e) {
        return ResultModel.SUCCESS(-1, "sql异常");
    }


    @ExceptionHandler(TemplateInputException.class)
    public String Exception(TemplateInputException e) {
        return "/error/404";

    }
}
